package com.tkjt.pms.server.common;


import com.tkjt.pms.modules.common.mapper.DepartUserChangeMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DepartUserChangeService {
    //往月部门负责人当前月份已删除
    public static final String CHANGE_WAY_1 = "1";
    //当前月份部门负责人往月不存在
    public static final String CHANGE_WAY_2 = "2";
    //当前月份部门负责人与往月负责人替换
    public static final String CHANGE_WAY_3 = "3";
    @Autowired
    private DepartUserChangeMapper departUserChangeMapper;

    /**
     * 获取所有部门负责人变更数据，部门code为key,value为部门下负责人变更集合
     * @param month
     * @return
     */
    public Map<String,Object> selectDepartUserChangeDataByMonth(String month){
        Map<String,Object> data = new HashMap<>();
        List<Map<String,Object>> list = departUserChangeMapper.selectDepartUserChangeDataByMonth(month);
        for (Map<String,Object> item : list){
            String indexDepart = item.get("indexDepart").toString();
            if(data.get(indexDepart) == null){
                List<Map<String,Object>> childDatas = new ArrayList<>();
                data.put(indexDepart,childDatas);
                childDatas.add(item);
            }else{
                List<Map<String,Object>> childDatas = (List<Map<String,Object>>)data.get(indexDepart);
                childDatas.add(item);
            }
        }
        return data;
    }

    /**
     * 替换部门负责人
     * @param departUserChangeMap
     * @param indexScorer
     * @param indexDepart
     * @return
     */
    public Map<String,Object> changeUser(Map<String,Object> departUserChangeMap,String indexDepart,String indexScorer){
        Map<String,Object> result = new HashMap<>();
        List<Map<String,Object>> childs = (List<Map<String,Object>>)departUserChangeMap.get(indexDepart);
        //该部门不存在负责人变更
        if(childs == null){
            result.put("isDo",false);
            return result;
        }
        List<Map<String,Object>> departUsers = new ArrayList<>();
        for(Map<String,Object> item : childs){
            //判断部门当前月份和往月是否存在负责人替换
            if(!"".equals(item.get("beforeUserCode").toString()) && !"".equals(item.get("afterUserCode").toString()) && indexScorer.equals(item.get("afterUserCode"))){
                departUsers.add(item);
                result.put("changeWay",CHANGE_WAY_3);
                result.put("departUsers",departUsers);
                result.put("isDo",true);
                return result;
            }
            //判断部门当前月份是否存在,往月不存在
            else if("".equals(item.get("beforeUserCode").toString()) && !"".equals(item.get("afterUserCode").toString()) && indexScorer.equals(item.get("afterUserCode"))){
                departUsers.add(item);
                result.put("changeWay",CHANGE_WAY_2);
                result.put("isDo",true);
                return result;
            }
            //判断部门是否存在往月部门负责人，当前月份删除了
            else if(!"".equals(item.get("beforeUserCode").toString()) && "".equals(item.get("afterUserCode").toString())){
                departUsers.add(item);
            }
        }
        if(departUsers.size() > 0){
            result.put("changeWay",CHANGE_WAY_1);
            result.put("departUsers",departUsers);
            result.put("isDo",true);
            return result;
        }else{
            result.put("isDo",false);
            return result;
        }
    }

    public String selectOldUserNameByParam(Map<String,Object> param){
        return departUserChangeMapper.selectOldUserNameByParam(param);
    }

    /**
     * 替换为往期部门负责人（绩效审核详情和绩效考核计算动态列使用）
     * @param indexYearMonth
     * @param orgs
     * @param newOrgs
     * @return
     */
    public List<Map<String,Object>> changeUserForCondition(String indexYearMonth,List<Map<String,Object>> orgs,List<Map<String,Object>> newOrgs){
        String yearMonth = "";
        if(indexYearMonth.length() == 8){
            yearMonth = indexYearMonth.substring(0,4)+indexYearMonth.substring(6);
        }else{
            yearMonth = indexYearMonth;
        }
        Map<String,Object> userDepartChangeMap = selectDepartUserChangeDataByMonth(yearMonth);
        Map<String, Object> addSignMap = new HashMap<>();
        for(Map<String, Object> org : orgs){
            String code = org.get("code").toString();
            String name = org.get("name").toString();
            int index = name.indexOf("(");
            String departName = name.substring(0,index);
            String indexDepart = code.substring(0,12);
            String indexScorer = code.substring(12);
            Map<String,Object> result = changeUser(userDepartChangeMap,indexDepart,indexScorer);
            if((Boolean) result.get("isDo") == true){
                if(result.get("changeWay").equals(DepartUserChangeService.CHANGE_WAY_1)){
                    if(addSignMap.get(indexDepart) == null){
                        List<Map<String, Object>> departUsers = (List<Map<String, Object>>)result.get("departUsers");
                        for(Map<String, Object> departUser : departUsers){
                            String beforeUserCode = departUser.get("beforeUserCode").toString();
                            String beforeUserName = departUser.get("beforeUserName").toString();
                            Map<String, Object> newOrgUser = new HashMap<>();
                            newOrgUser.put("code",indexDepart+beforeUserCode);
                            newOrgUser.put("name",departName+"("+beforeUserName+")");
                            newOrgs.add(newOrgUser);
                        }
                        addSignMap.put(indexDepart,1);
                    }
                }else if(result.get("changeWay").equals(DepartUserChangeService.CHANGE_WAY_2)){

                }else if(result.get("changeWay").equals(DepartUserChangeService.CHANGE_WAY_3)){
                    List<Map<String, Object>> departUsers = (List<Map<String, Object>>)result.get("departUsers");
                    String beforeUserCode = departUsers.get(0).get("beforeUserCode").toString();
                    String beforeUserName = departUsers.get(0).get("beforeUserName").toString();
                    Map<String, Object> newOrgUser = new HashMap<>();
                    newOrgUser.put("code",indexDepart+beforeUserCode);
                    newOrgUser.put("name",departName+"("+beforeUserName+")");
                    newOrgs.add(newOrgUser);
                }
            }else{
                newOrgs.add(org);
            }
        }
        return newOrgs;
    }

    /**
     * 替换为往期部门负责人（我的绩效报表使用）
     * @param indexYearMonth
     * @param departUserCodee
     * @return
     */
    public Map<String,Object> changeUserForMyReprot(String indexYearMonth,String departUserCodee,String titleName){
        Map<String,Object> userDepartChangeMap = selectDepartUserChangeDataByMonth(indexYearMonth);
        /*String indexDepart = departUserCodee.split("@")[0];
        String indexScorer = departUserCodee.split("@")[1];*/
        String indexDepart = "";
        String indexScorer = "";
        try {
            indexDepart = departUserCodee.split("@")[0];
            indexScorer = departUserCodee.split("@")[1];
        }catch (Exception e){
            e.printStackTrace();
        }
        Map<String,Object> result = changeUser(userDepartChangeMap,indexDepart,indexScorer);
        Map<String,Object> changeData = new HashMap<>();
        if((Boolean) result.get("isDo") == true){
            if(result.get("changeWay").equals(DepartUserChangeService.CHANGE_WAY_3)){
                List<Map<String, Object>> departUsers = (List<Map<String, Object>>)result.get("departUsers");
                String beforeUserCode = departUsers.get(0).get("beforeUserCode").toString();
                String beforeUserName = departUsers.get(0).get("beforeUserName").toString();
                changeData.put("beforeUserCode",beforeUserCode);
                if(StringUtils.isNotEmpty(titleName)){
                    changeData.put("titleName",titleName.substring(0,titleName.indexOf("("))+"("+beforeUserName+")");
                }
                return changeData;
            }else{
                return null;
            }
        }else{
            changeData.put("beforeUserCode",indexScorer);
            changeData.put("titleName",titleName);
            return changeData;
        }
    }

    /**
     * 替换为往期部门负责人（考核报表使用）
     * @param userDepartChangeMap
     * @param titleMap
     * @param dataDeparts
     * @param dataDepartMap
     */
    public void changeUserForLeaderReprot(Map<String,Object> userDepartChangeMap,Map<String,String> titleMap,List<String> dataDeparts,Map<String,Object> dataDepartMap){
        String indexScorer = dataDepartMap.get("code").toString().split("@")[1];
        String indexDepart = dataDepartMap.get("code").toString().split("@")[0];
        String departName = dataDepartMap.get("name").toString().substring(0,dataDepartMap.get("name").toString().indexOf("("));
        Map<String,Object> result = changeUser(userDepartChangeMap,indexDepart,indexScorer);
        Map<String, Object> addSignMap = new HashMap<>();
        if((Boolean) result.get("isDo") == true){
            if(result.get("changeWay").equals(DepartUserChangeService.CHANGE_WAY_1)){
                if(addSignMap.get(indexDepart) == null){
                    List<Map<String, Object>> departUsers = (List<Map<String, Object>>)result.get("departUsers");
                    for(Map<String, Object> departUser : departUsers){
                        String beforeUserCode = departUser.get("beforeUserCode").toString();
                        String beforeUserName = departUser.get("beforeUserName").toString();
                        dataDeparts.add(indexDepart+"@"+beforeUserCode);
                        titleMap.put(indexDepart+"@"+beforeUserCode,departName+"("+beforeUserName+")");
                    }
                    addSignMap.put(indexDepart,1);
                }
            }else if(result.get("changeWay").equals(DepartUserChangeService.CHANGE_WAY_2)){

            }else if(result.get("changeWay").equals(DepartUserChangeService.CHANGE_WAY_3)){
                List<Map<String, Object>> departUsers = (List<Map<String, Object>>)result.get("departUsers");
                String beforeUserCode = departUsers.get(0).get("beforeUserCode").toString();
                String beforeUserName = departUsers.get(0).get("beforeUserName").toString();
                dataDeparts.add(indexDepart+"@"+beforeUserCode);
                titleMap.put(indexDepart+"@"+beforeUserCode,departName+"("+beforeUserName+")");
            }
        }else{
            titleMap.put(dataDepartMap.get("code").toString(),dataDepartMap.get("name").toString());
            dataDeparts.add(dataDepartMap.get("code").toString());
        }
    }
}
